package com.ruoyi.iot.utils;

import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@Slf4j
public class WarpUtil {
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] dest = new byte[count];
        System.arraycopy(src, begin, dest, 0, count);
        return dest;
    }

    public static byte hexToByte(String hex) {
        hex = hex.toUpperCase();
        char[] hexChars = hex.toCharArray();
        return hexChars.length == 2 ? (byte) (charToByte(hexChars[0]) << 4 | charToByte(hexChars[1])) : charToByte(hexChars[0]);
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static String ascii2String(byte[] tBytes) {
        StringBuilder tStringBuf = new StringBuilder();
        char[] tChars = new char[tBytes.length];
        for (int i = 0; i < tBytes.length; i++) {
            tChars[i] = (char) tBytes[i];
        }
        tStringBuf.append(tChars);
        return tStringBuf.toString();
    }

    public static String byteToBit(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte by : bytes) {
            sb.append((by >> 7) & 0x1)
                    .append((by >> 6) & 0x1)
                    .append((by >> 5) & 0x1)
                    .append((by >> 4) & 0x1)
                    .append((by >> 3) & 0x1)
                    .append((by >> 2) & 0x1)
                    .append((by >> 1) & 0x1)
                    .append((by) & 0x1);
        }
        return sb.toString();
    }

    public static String byteToString(byte by) {
        return String.valueOf(by & 0x1) +
                ((by >> 1) & 0x1) +
                ((by >> 2) & 0x1) +
                ((by >> 3) & 0x1) +
                ((by >> 4) & 0x1) +
                ((by >> 5) & 0x1) +
                ((by >> 6) & 0x1) +
                ((by >> 7) & 0x1);
    }

    public static String twoBytesToString(byte by) {
        return String.valueOf(by & 0x3) +
                ((by >> 2) & 0x3) +
                ((by >> 4) & 0x3) +
                ((by >> 6) & 0x3);
    }

    public static byte[] bitToByte(String bit) {
        byte[] resBytes = new byte[0];

        if (null == bit || "".equals(bit)) {
            return resBytes;
        }

        int len = bit.length();
        // 只处理8bit
        if (len != 8) {
            return new byte[0];
        }
        int re;
        //正数
        if (bit.charAt(0) == '0') {
            re = Integer.parseInt(bit, 2);
        } else {
            //负数
            re = Integer.parseInt(bit, 2) - 256;
        }
        resBytes = Arrays.copyOf(resBytes, resBytes.length + 1);
        resBytes[resBytes.length - 1] = (byte) re;

        return resBytes;
    }

    public static byte[] getStringBytes(String string, int length) {
        byte[] bytes;
        if (string != null && string.length() == length) {
            bytes = string.getBytes(StandardCharsets.US_ASCII);
        } else {
            bytes = new byte[length];
        }
        return bytes;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte b : src) {
            stringBuilder.append(String.format("%02X", b)).append(" ");
        }
        return stringBuilder.toString();
    }

    public static String bytesToHexStringNoSpace(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte b : src) {
            stringBuilder.append(String.format("%02X", b));
        }
        return stringBuilder.toString();
    }

    public static String bytesTo0xHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte b : src) {
            stringBuilder.append(String.format("0x%02X", b)).append(", ");
        }
        return stringBuilder.toString();
    }

    public static Integer byteToInteger(byte b) {
        return 0xFF & b;
    }

    public static Integer shortToInteger(short b) {
        return 0xFFFF & b;
    }

    public static double shortToDouble(short b) {
        return 0xFFFF & b;
    }

    public static byte[] hexStringToBytes(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (charToByte(achar[pos]) << 4 | charToByte(achar[pos + 1]));
        }
        return result;
    }

    public static Integer bytesToInteger(byte[] src) {
        return Integer.parseInt(String.format("%02x", src[0])) * 100 + Integer.parseInt(String.format("%02x", src[1]));
    }

    public static byte[] integerToBytes(Integer i) {
        return new byte[]{Byte.parseByte(String.format("%02d", i / 100), 16), Byte.parseByte(String.format("%02d", i % 100), 16)};
    }

    public static String periodsBytesToInteger(byte[] src) {
        return String.format("%02x:%02x", src[1], src[0]);
    }

    public static byte[] periodsIntegerToBytes(String i) {
        return new byte[]{Byte.parseByte(i.substring(3, 5), 16), Byte.parseByte(i.substring(0, 2), 16)};
    }
}
